<?php

namespace app\api\controller\supplier;

use app\api\base\RestfulBase;
use app\modules\supplier\logic\OrdersLogic;
use Barcodegen\Barcode;

Class Orders extends RestfulBase
{
    protected $logic;

    protected function _initialize()
    {
        $this->logic = new OrdersLogic();
    }

    /**
     * 获取订单列表
     * @return Response
     */
    protected function index()
    {
        if ($this->request->isGet()) {

            $option = [
                'is_pages' => ($this->getParams('is_pages', 1, 'intval') >= 1 ? true : false),//是否分页查询
                'page_current' => $this->getParams('page_current', 1, 'intval'),//分页查询时，当前页码
                'page_size' => $this->getParams('page_size', 10, 'intval'),
                'order' => $this->getParams('order', 'DESC', 'trim,strip_tags,strtoupper'),
                'sort' => $this->getParams('sort', '', 'trim,strip_tags'),//排序字段
                'order_status'=>$this->getParams('order_status','','trim,strip_tags'),
                'keyword' => $this->getParams('keyword', '', 'trim,strip_tags'),
            ];

            if ($option['page_current'] < 1) {
                $option['page_current'] = 1;
            }

            $data = $this->logic->index($this->_account['supplier_id'],$option);

            if (is_array($data)) {
                return $this->apiSuccess($data);
            } else {
                return $this->apiError($this->logic->getError());
            }
        } else {
            return $this->apiError('无效请求');
        }
    }

    /**
     * 获取商品详情
     * @return Response
     */
    public function read()
    {
        if ($this->request->isGet()){
            $oid = $this->getParams('oid','','trim,strip_tags');

            $data = $this->logic->get($oid);

            if (is_array($data)){
                return $this->apiSuccess($data);
            }else{
                return $this->apiError($this->logic->getError());
            }
        }else{
            return $this->apiError('无效请求');
        }
    }

    /**
     * 打印条码
     * @return Response
     */
    public function print_brcode()
    {
        if ($this->request->isGet()){
            $oid = $this->getParams('oid','','trim,strip_tags');

            $data = $this->logic->PrintBrcode($oid);

            if (is_array($data)){
                return $this->apiSuccess($data);
            }else{
                return $this->apiError($this->logic->getError());
            }
        }else{
            return $this->apiError('无效请求');
        }
    }

    /**
     * 生成条码
     * @return Response
     */
    public function create_barcode(){
        if ($this->request->isGet()){

            vendor('Barcodegen.Barcode');
            $Barcode = new Barcode();

            $order_id = $this->getParams('order_id','','trim,strip_tags');
            $specval_id = $this->getParams('specval_id','','trim,strip_tags');

            $Barcode->draw(sprintf('%08s',$order_id.'-'.$specval_id))->show();exit;

        }else{
            return $this->apiError('无效请求');
        }
    }


    /**
     * 订单状态个数
     * @return Response
     */
    public function get_order_status_num(){
        if($this->request->isGet()){
            $order_status = $this->getParams('order_status','','trim,strip_tags');
            $data = $this->logic->getOrderStatusNum($order_status);
            if(is_array($data)){
                return $this->apiSuccess($data);
            }else{
                return $this->apiError($this->logic->getError());
            }

        }else{
            return $this->apiError('无效请求！');
        }

    }

    /**
     * 订单状态更改
     * @return Response
     */
    public function order_status_change(){
         if($this->request->isPost()){
             $oid = $this->getParams('oid','','trim,strip_tags');
             $order_status = $this->getParams('order_status','','trim,strip_tags');

             if($this->logic->OrderStatusChange($oid,$order_status)){
                 return $this->apiSuccess();
             }else{
                 return $this->apiError($this->logic->getError());
             }

         }else{
             return $this->apiError('无效请求！');
         }

    }

    /**
     * 订单商品状态更改
     * @return Response
     */
    public function order_goods_status_change(){
        if($this->request->isPost()){

            $order_goods_id = $this->getParams('order_goods_id','','trim,strip_tags');
            $status = $this->getParams('status','','trim,strip_tags');
            $num  = $this->getParams('num','','intval');

            if($this->logic->OrderGoodsStatusChange($order_goods_id,$status,$num)){
                return $this->apiSuccess();
            }else{
                return $this->apiError($this->logic->getError());
            }

        }else{
            return $this->apiError('无效请求！');
        }
    }

}

